<?php

/*----------------- Shopping CART --------------------------------------*/
/*
	add item to cart
	cart item has following structure
	
	category id, product id, unit price, quantity (no duplicated item in cart)
	return: true, false
*/	
/*------------------------------------------------------------------------*/
session_start();

function addUpdateCart($catid, $prodid, $uprice, $qty, $prodname, $desc)
{

	if (isset($_SESSION['cart']) == false)
	{
		//showArr($_SESSION['cart']);
		//exit;

		// add net new
		$_SESSION['cart'] = array();
		
		$newArray = array();
		
		$newArray['cat_id'] = $catid;
		$newArray['product_id'] = $prodid;
		$newArray['unit_price'] = $uprice;
		$newArray['qty'] = $qty;
		$newArray['product_name'] = $prodname;	
		$newArray['product_desc'] = $desc;
		//$newArray['shipping_fee'] = 0;
		//$newArray['shipping_country'] = '';			
		//$newArray['tax_fee'] = 0;
		//$newArray['tax_pro'] = '';				
		
		array_push($_SESSION['cart'], $newArray);
	}
	else 
	{
		// update existing
		$isExist = false;
		
		foreach ($_SESSION['cart'] as $key => $arr)
		{
			// looking for category id and product id match
			if ($arr['cat_id'] == $catid && $arr['product_id'] == $prodid){
				//$arr[0] = $catid;
				//$arr[1] = $prodid;
				//$arr[2] = $uprice;
				$arr['qty'] = $arr['qty'] + $qty;
				//$arr[4] = $prodname;
				
				$_SESSION['cart'][$key] = $arr;
				$isExist = true;
			}
		}
		
		if ($isExist == false)
		{
			$twoArray = array();
			$twoArray['cat_id'] = $catid;
			$twoArray['product_id'] = $prodid;
			$twoArray['unit_price'] = $uprice;
			$twoArray['qty'] = $qty;
			$twoArray['product_name'] = $prodname;
			$twoArray['product_desc'] = $desc;	
			//$newArray['shipping_fee'] = 0;
			//$newArray['shipping_country'] = '';			
			//$newArray['tax_fee'] = 0;
			//$newArray['tax_pro'] = '';							

			array_push($_SESSION['cart'], $twoArray);
		}
	}
}

// cart quantities update
function cartQtyUpdate($catid, $prodid, $qty)
{
	if (isset($_SESSION['cart']))
	{
		// update existing		
		foreach ($_SESSION['cart'] as $key => $arr)
		{
			// looking for category id and product id match
			if ($arr['cat_id'] == $catid && $arr['product_id'] == $prodid)
			{
				$arr['qty'] = $qty;			
				$_SESSION['cart'][$key] = $arr;
			}
		}		
	}
}


// cart tax update
function cartTaxUpdate($catid, $prodid, $tax_fee, $tax_pro)
{
	if (isset($_SESSION['cart']))
	{
		// update existing		
		foreach ($_SESSION['cart'] as $key => $arr)
		{
			// looking for category id and product id match
			if ($arr['cat_id'] == $catid && $arr['product_id'] == $prodid)
			{
				$arr['tax_fee'] = $tax_fee;			
				$arr['tax_pro'] = $tax_pro;
				$_SESSION['cart'][$key] = $arr;
			}
		}		
	}
}

// cart shipping update
function cartShippingUpdate($catid, $prodid, $shipping_fee, $shipping_country)
{
	if (isset($_SESSION['cart']))
	{
		// update existing		
		foreach ($_SESSION['cart'] as $key => $arr)
		{
			// looking for category id and product id match
			if ($arr['cat_id'] == $catid && $arr['product_id'] == $prodid)
			{
				$arr['shipping_fee'] = $shipping_fee;			
				$arr['shipping_country'] = $shipping_country;
				$_SESSION['cart'][$key] = $arr;
			}
		}		
	}
}


/*
	get Cart quantities.
*/
function getCartTotalQtys()
{
	$totalQty = 0;
	if (isset($_SESSION['cart']))
	{
		foreach ($_SESSION['cart'] as $key => $arr)
		{
			$totalQty = $totalQty + $arr['qty'];
		}
	}
	return $totalQty;
}


/*
	delete item from cart by category id and product id
*/
function delCart($catid, $prodid){
	foreach ($_SESSION['cart'] as $key => $arr){
		// looking for category id and product id match
		if ($arr['cat_id'] == $catid && $arr['product_id'] == $prodid){
			unset($_SESSION['cart'][$key]);
		}
	}
}

/*
	return subtotal or false
*/
function cartItemSubtotal($catid, $prodid){
	$result = false;
	if (isset($_SESSION['cart']))
	{
		foreach ($_SESSION['cart'] as $arr){
			// looking for category id and product id match
			if ($arr['cat_id'] == $catid && $arr['product_id'] == $prodid){
				$result = $arr['qty'] *	$arr['unit_price'];
			}
		}
	}
	return mny_format($result);
}

/*
	return grand total or false
*/
function cartItemGrandTotal(){
	$result = false;
	if (isset($_SESSION['cart']))
	{	
		foreach ($_SESSION['cart'] as $key => $arr)
		{
			$result += $arr['qty'] *	$arr['unit_price'];
		}
	}
	return mny_format($result);
}



// Get New Cart Table from here.
/*
	output shopping cart list
*/

function cartList()
{
	$header = getCartTableHeader();
	$emptyLine = getCartTableEmptyLine(); 
	$footer = '';
	$body = '';
	$str = '';
	$frm = '';

	$name_line = '';
	$total_qty = 0;
	$total_price = 0;

	
	if (isset($_SESSION['cart']) && count($_SESSION['cart']) != 0)
	{
		$count = 1;
		
		$frm .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="payForm">';
		$frm .= '	<input type="hidden" name="cmd" value="_cart"/>';
		$frm .= '<input type="hidden" name="upload" value="1">';
		$frm .= '	<input type="hidden" name="business" value="9KV6R5HVXBSRJ"/>';
		$frm .= '	<input type="hidden" name="lc" value="CA"/>';  

		// Cart Table Body
		foreach ($_SESSION['cart'] as $arr)
		{
			//$arr = $_SESSION['cart'][$n];
			$str .= '	    <tr>';
			$str .= '	    	<input type="hidden" name="prodid_'.$arr['product_id'].'" id="prodid_'.$arr['product_id'].'" value="'.$arr['product_id'].'"/>';
			$str .= '	    	<input type="hidden" name="prodname_'.$arr['product_id'].'" id="prodname_'.$arr['product_id'].'" value="'.$arr['product_name'].'"/>';			
			$str .= '	    	<input type="hidden" name="unitprice_'.$arr['product_id'].'" id="unitprice_'.$arr['product_id'].'" value="'.$arr['unit_price'].'"/>';						
			$str .= '	    	<td class="cartList">'.$arr['product_id'].'</td>';
			$str .= '	        <td class="cartList">'.$arr['product_name'].'</td>';
			$str .= '	        <td class="cartList">'.$arr['product_desc'].'</td>';			
			$str .= '	        <td class="cartList">';
			$str .= '	        <input type="text" name="qty_'.$arr['product_id'].'" id="qty_'.$arr['product_id'].'" value="'.$arr['qty'].'" size="6" maxlength="4" width="50px" onkeyup="stripChar('.$arr['product_id'].');"/>';
			$str .= '	        </td>	';		
			$str .= '	        <td class="cartList">';
			$str .= '	    	   	<span id="uprice">'.$arr['unit_price'].'</span>';
			$str .= '	        </td>	';
			$str .= '	        <td class="cartList">';
			$str .= '	        	<span id="subtotal">	'.cartItemSubtotal($arr['cat_id'], $arr['product_id']).'</span>';
			$str .= '	        </td>	';
			$str .= '	        <td class="cartList">';
			$str .= '	        	<a href="cart.php?action=remove&cid='.$arr['product_id'].'&pid='.$arr['product_id'].'">Remove</a>';
			$str .= ' | ';			
			$str .= '	        	<a id="update_'.$arr['product_id'].'" href="cart.php?action=update&cid='.$arr['product_id'].'&pid='.$arr['product_id'].'">Update</a>';			
			$str .= '	         </td>';	  
			$str .= '	    </tr>';
			
			$frm .= '	<input type="hidden" name="item_name_'.$count.'" value="'.$arr['product_name'].'"/>';
			$frm .= '	<input type="hidden" name="item_number_'.$count.'" value="'.$arr['product_desc'].'"/>'; // Description	
			$frm .= '	<input type="hidden" name="amount_'.$count.'" value="'.$arr['unit_price'].'"/>';
			$frm .= '	<input type="hidden" name="quantity_'.$count.'" value="'.$arr['qty'].'" />';   		
			$count = $count + 1;						
		}				
		
		$body = $str;
		$footer = getCartTableFooter();
		
		$frm .= '	<input type="hidden" name="currency_code" value="CAD"/>';
		$frm .= '	<input type="hidden" name="button_subtype" value="services"/>';
		$frm .= '	<input type="hidden" name="no_note" value="1"/>';
		$frm .= '	<input type="hidden" name="no_shipping" value="1"/>';
		$frm .= '	<input type="hidden" name="rm" value="1"/>';
		$frm .= '	<input type="hidden" name="return" value="http://www.888accessories.ca"/>';
		$frm .= '	<input type="hidden" name="cancel_return" value="http://www.888accessories.ca/"/>';
		$frm .= '	<input type="hidden" name="bn" value=""/>';
		$frm .= '	<input type="hidden" name="custom" value="" />';        
		$frm .= '</form>';
	}	
	else
	{
	   	$body = getCartTableBody();
	   	$footer = getCartTableFooter();
	}
	
	return $header.$body.$emptyLine.$footer.$frm;
}

function cartList888()
{

	$footer = '';
	$body = '';
	$str = '';
	$frm = '';

	$name_line = '';
	$total_qty = 0;
	$total_price = 0;

	
	if (isset($_SESSION['cart']) && count($_SESSION['cart']) != 0)
	{
		// Cart Table Body
		foreach ($_SESSION['cart'] as $arr)
		{
			//$arr = $_SESSION['cart'][$n];
			$str .= '	    <tr>';
			$str .= '	    	<input type="hidden" name="prodid_'.$arr['product_id'].'" id="prodid_'.$arr['product_id'].'" value="'.$arr['product_id'].'"/>';
			$str .= '	    	<input type="hidden" name="prodname_'.$arr['product_id'].'" id="prodname_'.$arr['product_id'].'" value="'.$arr['product_name'].'"/>';			
			$str .= '	    	<input type="hidden" name="unitprice_'.$arr['product_id'].'" id="unitprice_'.$arr['product_id'].'" value="'.$arr['unit_price'].'"/>';						
			$str .= '	    	<td width="100" height="25" align="center" valign="middle" class="body2text4" >'.$arr['product_id'].'</td>';
			$str .= '	        <td width="325" align="center" valign="middle" class="body2text4" >'.$arr['product_name'].'</td>';
			$str .= '	        <td width="45" align="center" valign="middle" class="body2text4">';
			$str .= '	        <input type="text" name="qty_'.$arr['product_id'].'" id="qty_'.$arr['product_id'].'" value="'.$arr['qty'].'" size="4" maxlength="4" width="50px" onkeyup="stripChar('.$arr['product_id'].');"/>';
			$str .= '	        </td>	';		
			$str .= '	        <td width="125" align="center" valign="middle" class="body2text4" >';
			$str .= '	    	   	$<span id="uprice">'.$arr['unit_price'].'</span>';
			$str .= '	        </td>	';
			$str .= '	        <td width="125" align="center" valign="middle" class="body2text4" >';
			$str .= '	        	<a href="checkout.php?action=remove&cid='.$arr['product_id'].'&pid='.$arr['product_id'].'"><u>Remove</u></a>';
			$str .= ' | ';			
			$str .= '	        	<a id="update_'.$arr['product_id'].'" href="checkout.php?action=update&cid='.$arr['product_id'].'&pid='.$arr['product_id'].'"><u>Update</u></a>';			
			$str .= '	         </td>';	  
			$str .= '	    </tr>';						
		}				
		
		$body = $str;
		$footer = getCartTableFooter();		
	}	

	
	return $body.$frm;
}


function getCartTableHeader()
{
    $str = '<form name="cart_form" id="cart_form" action="cart.php" method="post">';
    $str .= '	<table class="cartTable" border="1px" cellspacing="0" bordercolor="#CCCCCC" bgcolor="#FFFFFF">';
    $str .= '	    <!-- header -->';
    $str .= '	    <tr>';
    $str .= '	    	<td class="cartListHeader">';
    $str .= '	            Product ID';
    $str .= '	        </td>';
    $str .= '	        <td class="cartListHeader">';
    $str .= '	        	Product Name';
    $str .= '	    	</td>	';																																																																																																																																																																																																																																																																																																																																																																																																																																																			
    $str .= '	        <td class="cartListHeader">';
    $str .= '	        	Description';
    $str .= '	    	</td>	';																																																																																																																																																																																																																																																																																																																																																																																																																																																				
    $str .= '	    	<td class="cartListHeader">';
    $str .= '	        	Quantity';
    $str .= '	    	</td>';
    $str .= '	    	<td class="cartListHeader">';
    $str .= '	        	Unit Price';
    $str .= '	    	</td>';
    $str .= '	    	<td class="cartListHeader">';
    $str .= '	        	Subtotal';
    $str .= '	    	</td>';
    $str .= '	    	<td class="cartListHeader">';
    $str .= '	        	Action';
    $str .= '	    	</td>';
    $str .= '	    </tr>';
	
	return $str;
}  
  
function getCartTableBody()
{
    $str = '	    <tr>';
    $str .= '	    	<input type="hidden" name="catid" id="catid" value=""/>';
    $str .= '	    	<input type="hidden" name="prodid" id="prodid" value=""/>';
    $str .= '	    	<td class="cartList">&nbsp;';
    $str .= '	        </td>	';
	$str .= '	        <td class="cartList">&nbsp;';
    $str .= '	        </td>	';
	$str .= '	        <td class="cartList">&nbsp;';
    $str .= '	        </td>	';
    $str .= '	        <td class="cartList">';
    $str .= '	        <input type="text" name="qty" id="qty" value="" size="6" maxlength="4" width="50px" onkeyup="stripChar();"/>';
    $str .= '	        </td>	';		
    $str .= '	        <td class="cartList">';
    $str .= '	    	   	<span id="uprice">';
    $str .= '	       		</span>';
    $str .= '	        </td>	';
	$str .= '	        <td class="cartList">';
	$str .= '	        	<span id="subtotal">	';
	$str .= '	            </span>';
	$str .= '	        </td>	';
	$str .= '	        <td class="cartList">';
	$str .= '	        	<a href="cart.php?action=remove&catid=&prodid=">Remove</a>';
	$str .= '	         </td>';	  
	$str .= '	    </tr>';
	
	return $str;
}		
    
function getCartTableEmptyLine()
{   
	$str = '	<!-- empty line -->';
    $str .= '    <tr>';
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';  
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';        
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';            
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';            	
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';            
    $str .= '        <td class="cartListEmpty">&nbsp;</td>';                   
    $str .= '    </tr>';
	
	return $str;
 }
 
function getCartTableFooter()
{   
	$str = '	<!-- footer -->';
    $str .= '  	  <tr class="cartListFooter">';
    $str .= '        <td>&nbsp;</td>';
    $str .= '        <td>&nbsp;</td>';
    $str .= '        <td>Total:</td>';
    $str .= '        <td>'.getCartTotalQtys().'</td>';
    $str .= '        <td>';
    $str .= '        	<span id="grandtotal">';
    $str .= '        	</span>';
    $str .= '        </td>';
    $str .= '        <td>'.cartItemGrandTotal().'</td>';
   	$str .= '         <td>&nbsp;';
    $str .= '        </td>';
    $str .= '  	  </tr>';
    $str .= '	<!-- table end -->';
    $str .= '	</table>';
	$str .= '</form>';
	$str .= '<br/>';		
	$str .='<input type="submit" name="action" value="Checkout" onclick="return doCheckout();"/>';
	$str .='&nbsp;';
	$str .='&nbsp;';
	$str .='<input type="button" name="toprod" value="Continue Shopping" onclick="toHomePage();"/>';
	
	return $str;
}

function getPaypalForm()
{
	$str = '<form action="" method="post" id="payForm">';
	$str = '	<input type="hidden" name="cmd" value=""/>';
	$str = '	<input type="hidden" name="business" value=""/>';
	$str = '	<input type="hidden" name="lc" value=""/>';   
	$str = '	<input type="hidden" name="item_name" value=""/>';
	$str = '	<input type="hidden" name="item_number" value=""/>';
	$str = '	<input type="hidden" name="amount" value=""/>';
	$str = '	<input type="hidden" name="quantity" value="" />';   
	$str = '	<input type="hidden" name="currency_code" value=""/>';
	$str = '	<input type="hidden" name="button_subtype" value=""/>';
	$str = '	<input type="hidden" name="no_note" value=""/>';
	$str = '	<input type="hidden" name="no_shipping" value=""/>';
	$str = '	<input type="hidden" name="rm" value=""/>';
	$str = '	<input type="hidden" name="return" value=""/>';
	$str = '	<input type="hidden" name="cancel_return" value=""/>';
	$str = '	<input type="hidden" name="bn" value=""/>';
	$str = '	<input type="hidden" name="custom" value="" />';        
	$str = '</form>';
}


/*-------------- Disabled methods ------------------*/

/*
      	<!-- unlisted items section -->
        <br/>
  		<div class="cartListFooter">
		Unlisted Items - please enter the product code and required quantity to receive a quotation
        </div>
		<table class="cartTable" border="1px" cellspacing="0" bordercolor="#CCCCCC" bgcolor="#FFFFFF">
			<tr>
				<td class="cartListHeader">Product Name</td>
				<td class="cartListHeader">Quantity</td>
			</tr>
			<tr>
				<td class="cartList"><input type='text' name='unlistedProdCode' id='unlistedProdCode' value='' size='30' maxlength='10'/></td>
				<td class="cartList">
        			<input type='text' name='unlistedQuantity' id='unlistedQuantity' width="100px" value='' size='10' maxlength='4' onkeyup='stripChar(this);getTotal();'/>        		</td>
			</tr>
		</table>
  		<br/>        
		<input type='submit' name='action' value='Checkout' onclick='return doCheckout()'/>
		&nbsp;
		<input type='submit' name='action' value='Update' onclick='return doUpdate()'/>
		&nbsp;
		<input type='button' name='toprod' value='Continue Shopping' onclick='toProd()'/>

*/


/*
	output shopping cart list

function cartList(){
	if (isset($_SESSION['cart']) && count($_SESSION['cart']) != 0){
		// table begin
		print('<table>');

		// header
		print('<tr>');
		print('<td>');
		print('Product ID');
		print('</td>');
		print('<td>');
		print('Product Name');
		print('</td>');
		print('<td>');
		print('Quantity');
		print('</td>');
		print('<td>');
		print('Unit Price');
		print('</td>');
		print('<td>');
		print('Subtotal');
		print('</td>');
		print('<td>');
		print('Action');
		print('</td>');
		print('</tr>');		
	
		$count = 0;
		foreach ($_SESSION['cart'] as $arr){
			print('<tr>');
		
  		print('<input type=\'hidden\' name=\'catid'.$count.'\' id=\'catid'.$count.'\' value=\''.$arr[0].'\'/>');
  		print('<input type=\'hidden\' name=\'prodid'.$count.'\' id=\'prodid'.$count.'\' value=\''.$arr[1].'\'/>');

  		print('<td>');
  		print($arr[1]);
  		print('</td>');

  		print('<td>');
  		print($arr[4]);
  		print('</td>');
  
  		print('<td>');
  		print('<input type=\'text\' name=\'qty'.$count.'\' id=\'qty'.$count.'\' value=\''.$arr[3].'\' size=\'4\' maxlength=\'4\' onkeyup=\'stripChar(this);getTotal();\'/>');
  		print('</td>');
  		
  		print('<td>');
  		print('<span id=\'uprice'.$count.'\'>');
  		print($arr[2]);
  		print('</span>');
  		print('</td>');
  
  		print('<td>');
  		print('<span id=\'subtotal'.$count.'\'>');
  		print(cartItemSubtotal($arr[0],$arr[1]));
  		print('</span>');
  		print('</td>');
  
  		print('<td>');
  		print('<a href=\'cart.php?action=remove&catid='.$arr[0].'&prodid='.$arr[1].'\'>Remove</a>');
  		print('</td>');
  
  		print('</tr>');
  		
  		$count++;
  	}
  
		// empty line
		print('<tr>');
		print('<td>');
		print('');
		print('</td>');
		print('<td>');
		print('');
		print('</td>');
		print('<td>');
		print('');
		print('</td>');
		print('<td>');
		print('');
		print('</td>');
		print('<td>');
		print('');
		print('</td>');
		print('</tr>');

		// footer
		print('<tr>');
		print('<td>');
		print('');
		print('</td>');
		print('<td>');
		print('');
		print('</td>');
		print('<td>');
		print('Total');
		print('</td>');
		print('<td>');
		print('<span id=\'grandtotal\'>');
		print(cartItemGrandTotal());
		print('</span>');
		print('</td>');
		print('<td>');
		print('');
		print('</td>');
		print('</tr>');

  	// table end
  	print('</table>');
  	
  	// unlisted items section
  	// title
		print('Unlisted Items - please enter the product code and required quantity to receive
a quotation');
		print('<table>');
		print('<tr>');
		print('<td>Product Name</td>');
		print('<td>Quantity</td>');
		print('</tr>');
		print('<tr>');
		print('<td><input type=\'text\' name=\'unlistedProdCode\' id=\'unlistedProdCode\' value=\'\' size=\'10\' maxlength=\'10\'/></td>');
		print('<td><input type=\'text\' name=\'unlistedQuantity\' id=\'unlistedQuantity\' value=\'\' size=\'4\' maxlength=\'4\' onkeyup=\'stripChar(this);getTotal();\'/></td>');
		print('</tr>');
		print('</table>');
  	
		print('<input type=\'submit\' name=\'action\' value=\'Checkout\' onclick=\'return doCheckout()\'/>');
		print('&nbsp;');
		print('<input type=\'submit\' name=\'action\' value=\'Update\' onclick=\'return doUpdate()\'/>');
		print('&nbsp;');
		print('<input type=\'button\' name=\'toprod\' value=\'Continue Shopping\' onclick=\'toProd()\'/>');
	}
	else {
  	// unlisted items section
  	// title
		print('Unlisted Items - please enter the product code and required quantity to receive
a quotation');
		print('<table>');
		print('<tr>');
		print('<td>Product Name</td>');
		print('<td>Quantity</td>');
		print('</tr>');
		print('<tr>');
		print('<td><input type=\'text\' name=\'unlistedProdCode\' id=\'unlistedProdCode\' value=\'\' size=\'10\' maxlength=\'10\'/></td>');
		print('<td><input type=\'text\' name=\'unlistedQuantity\' id=\'unlistedQuantity\' value=\'\' size=\'4\' maxlength=\'4\' onkeyup=\'stripChar(this);getTotal();\'/></td>');
		print('</tr>');
		print('</table>');
  	
		print('<input type=\'submit\' name=\'action\' value=\'Checkout\' onclick=\'return doCheckout()\'/>');
		print('&nbsp;');
		print('<input type=\'submit\' name=\'action\' value=\'Update\' onclick=\'return doUpdate()\'/>');
		print('&nbsp;');
		print('<input type=\'button\' name=\'toprod\' value=\'Continue Shopping\' onclick=\'toProd()\'/>');

		//print('Shopping cart is empty.');
	}
}
*/
/*----------------------------------------------------*/


?>